package cn.wuyu.baselibrary.net.rx;

import android.net.ParseException;

import com.google.gson.JsonParseException;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import java.net.ConnectException;

import cn.wuyu.baselibrary.event.LoginEvent;
import cn.wuyu.baselibrary.net.http.HttpException;
import cn.wuyu.baselibrary.utils.NetworkUtils;
import cn.wuyu.baselibrary.utils.ToastUtil;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
//import org.reactivestreams.Subscriber;

/**
 * author : xuzongmeng
 * date   : 2018/8/3
 * desc   : 酒店 机票   Observer
 */

public class OnNextObserver<T> implements Observer<T> {
    private Disposable mDisposable;
    @SuppressWarnings("unchecked")
    @Override
    public void onError(Throwable e) {
        Logger.e("====订阅===onError" + e.toString());
        if (e instanceof HttpException) {
            ToastUtil.show(HttpException.Companion.getMessage());
            if ("10-000003".equals(HttpException.Companion.getErrorCode())) {
                EventBus.getDefault().post(new LoginEvent(false));
//                ActivityUtils.startActivity();
//                10-000003
//                AppUtil.getACache().clear();
//                SpUtil.clean();
            }
        } else if (e instanceof JsonParseException || e instanceof JSONException
                || e instanceof ParseException) {
            ToastUtil.show("解析错误");
        } else if (e instanceof ConnectException) {
            ToastUtil.show("连接失败");
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ToastUtil.show("证书验证失败");
        } else if (e instanceof java.net.SocketTimeoutException) {
            ToastUtil.show("连接超时");
        } else if (e instanceof java.net.UnknownHostException) {
            ToastUtil.show("无法连接服务器");
        } else {
            ToastUtil.show("未知错误" + e.getMessage());
        }
        onFailed(e);
    }

    @Override
    public void onComplete() {
        if (!mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }


    @Override
    public void onSubscribe(Disposable d) {
//        Logger.d("=====订阅==onSubscribe==" );
        mDisposable = d;
        if (!NetworkUtils.isConnected()) {
            onError(new HttpException("00000001", "未连接网络"));
            mDisposable.dispose();
        }
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);

    }


    public void onSuccess(T t) {

    }

    public void onFailed(Throwable e) {

    }


    /**
     * 约定异常
     */
    class ERROR {
        /**
         * 未知错误
         */
        public static final String UNKNOWN = "100000";
        /**
         * 解析错误
         */
        public static final String PARSE_ERROR = "100001";
        /**
         * 网络错误
         */
        public static final String NETWORD_ERROR = "100002";
        /**
         * 协议出错
         */
        public static final String HTTP_ERROR = "100003";

        /**
         * 证书出错
         */
        public static final String SSL_ERROR = "100005";

        /**
         * 连接超时
         */
        public static final String TIMEOUT_ERROR = "100006";
        /**
         * 无法连接到服务器
         */
        public static final String UNKNOWN_HOST = "100007";
    }

}

